package com.stn.zjx.givingtreasure.activity.bi;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.came.viewbguilib.ButtonBgUi;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.google.gson.Gson;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.progresslayout.ProgressLayout;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.adapter.EthTransferListAdapter;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.bean.EthTransfer;
import com.stn.zjx.givingtreasure.domain.EthTransferHistoryDomain;
import com.stn.zjx.givingtreasure.domain.GetEthBalanceDomain;
import com.stn.zjx.givingtreasure.event.EthTransferEvent;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;
import com.stn.zjx.givingtreasure.net.BaseApi;
import com.stn.zjx.givingtreasure.net.impl.EthNetImpl;
import com.stn.zjx.givingtreasure.utils.StatusBarUtil;
import com.vondear.rxtools.RxActivityTool;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BHistoryActivity extends BaseActivity {
    @BindView(R.id.back_bt)
    RelativeLayout backBt;
    @BindView(R.id.action_title)
    TextView actionTitle;
    @BindView(R.id.web_title)
    TextView webTitle;
    @BindView(R.id.action_add_bt)
    RelativeLayout actionAddBt;
    @BindView(R.id.action_refresh)
    RelativeLayout actionRefresh;
    @BindView(R.id.exit)
    RelativeLayout exit;
    @BindView(R.id.sure_bt)
    TextView sureBt;
    @BindView(R.id.skip_bt)
    TextView skipBt;
    @BindView(R.id.share_bt)
    RelativeLayout shareBt;
    @BindView(R.id.more_bt)
    RelativeLayout moreBt;
    @BindView(R.id.release_bt)
    ButtonBgUi releaseBt;
    @BindView(R.id.search_iv)
    ImageView searchIv;
    @BindView(R.id.search_edit)
    EditText searchEdit;
    @BindView(R.id.search_bg)
    RelativeLayout searchBg;
    @BindView(R.id.save_bt)
    TextView saveBt;
    @BindView(R.id.bottom)
    LinearLayout bottom;
    @BindView(R.id.head_total)
    TextView headTotal;
    @BindView(R.id.head_total_sum)
    TextView headTotalSum;
    @BindView(R.id.lineChart)
    LineChart lineChart;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.twinkling_refresh)
    TwinklingRefreshLayout twinklingRefresh;
    public String address = "";
    @BindView(R.id.scan_bt)
    RelativeLayout scanBt;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    private String tokenAddress = "";
    private String tokenSymbol = "";
    private Integer tokenDecimal = 0;
    Integer pageNow = 1;
    Integer pageSize = 10;
    private LinearLayoutManager lm;
    private EthTransferListAdapter adapter;
    private boolean isMoreLoading = false;
    private Integer visibleItemCount = 0;


    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void TransferEvent(EthTransferEvent ethTransferEvent){
             switch (ethTransferEvent.flag){
                 case EthTransferEvent.TRANSFER_FINISH:
                      twinklingRefresh.startRefresh();
                     break;
             }
    }
    @Override
    public void initComponent() {
        address = getIntent().getStringExtra("address");
        tokenAddress = getIntent().getStringExtra("tokenAddress");
        tokenSymbol = getIntent().getStringExtra("tokenSymbol");
        tokenDecimal = getIntent().getIntExtra("tokenDecimal", 0);
        actionTitle.setText(tokenSymbol);
        initChart();
        rv.setLayoutManager(lm = new LinearLayoutManager(this, OrientationHelper.VERTICAL, false));
        adapter = new EthTransferListAdapter(this);
        rv.setAdapter(adapter);
        rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (!isMoreLoading && newState == RecyclerView.SCROLL_STATE_IDLE && visibleItemCount == adapter.dataBeanList.size() - 1 &&
                        adapter.dataBeanList.size() >= pageNow * 10) {
                    pageNow++;
                    initAppend(tokenAddress, address, pageNow, pageSize);
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                visibleItemCount = lm.findLastVisibleItemPosition();

            }
        });
        ProgressLayout headerView = new ProgressLayout(this);
        headerView.setColorSchemeResources(R.color.colorAccent);
        twinklingRefresh.setHeaderView(headerView);
        twinklingRefresh.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
                pageNow = 1;
                initRefresh(tokenAddress, address, pageNow, pageSize);
            }
        });
        twinklingRefresh.startRefresh();
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b_history);
        StatusBarUtil.setImmersiveStatusBar(this, true);
        ButterKnife.bind(this);
        initComponent();
    }

    public void initChart() {
        //设置描述文本不显示
        Legend legend = lineChart.getLegend();
        legend.setPosition(Legend.LegendPosition.ABOVE_CHART_RIGHT);
        lineChart.getDescription().setEnabled(false);
        // lineChart.setNoDataText("");
        lineChart.setNoDataTextColor(getResources().getColor(R.color.translucent));
        // lineChart.getDescription().setText("");
        lineChart.getDescription().setTextColor(getResources().getColor(R.color.translucent));
        //设置是否显示表格背景

        //设置是否可以触摸
        lineChart.setTouchEnabled(true);
        lineChart.setDragDecelerationFrictionCoef(0.9f);
        //设置是否可以拖拽
        lineChart.setDragEnabled(true);
        //设置是否可以缩放
        lineChart.setScaleEnabled(false);
        lineChart.setDrawGridBackground(false);
        lineChart.setHighlightPerDragEnabled(true);
        lineChart.setPinchZoom(true);
        //设置背景颜色
        lineChart.setBackgroundColor(Color.WHITE);


        XAxis xAxis = lineChart.getXAxis();
        xAxis.setDrawGridLines(false);

        /*解决左右两端柱形图只显示一半的情况 只有使用CombinedChart时会出现，如果单独使用BarChart不会有这个问题*/
//        xAxis.setAxisMinimum(0);
//        xAxis.setAxisMaximum(10);
        //   xAxis.setLabelCount(4);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); // 设置X轴标签位置，BOTTOM在底部显示，TOP在顶部显示
        //    xAxis.setLabelCount(domain.getData().getList().size()); // 设置X轴标签数量
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                SimpleDateFormat sdf= new SimpleDateFormat("MM/dd");
                Date date=new Date();
                int v = (int) value;
                return sdf.format(date) + "";
            }
        });
        YAxis axisLeft = lineChart.getAxisLeft(); // 获取左边Y轴操作类
        //   axisLeft.setAxisMinimum(0); // 设置最小值
        //                            axisLeft.setGranularityEnabled(true);
//                              axisLeft.setGranularity(10); // 设置Label间隔
        //  axisLeft.setLabelCount(10);// 设置标签数量
        axisLeft.setDrawGridLines(false);
        YAxis rightYAxis = lineChart.getAxisRight();
        rightYAxis.setEnabled(false);
        rightYAxis.setDrawGridLines(true);
        rightYAxis.enableGridDashedLine(10f, 10f, 0f);
        axisLeft.resetAxisMinimum();
        axisLeft.resetAxisMaximum();

        List<Entry> entryList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Entry entry = null;
            entry = new Entry(i, new BigDecimal((0.0)).floatValue());
            entryList.add(entry);
        }
        axisLeft.setDrawGridLines(false);

        LineDataSet lineDataSet = new LineDataSet(entryList, getString(R.string.transaction));
        lineDataSet.setDrawCircleHole(false);
        // 设置平滑曲线
        lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        // 不显示坐标点的小圆点
        lineDataSet.setDrawCircles(false);
        lineDataSet.setDrawValues(false);
        lineDataSet.setFillColor(ActivityCompat.getColor(this, R.color.colorAccent));
        lineDataSet.setColor(ActivityCompat.getColor(this, R.color.colorAccent));
        //设置填充
        //设置允许填充
        lineDataSet.setDrawFilled(true);
        lineDataSet.setFillAlpha(50);
        //设置渐变
        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.line_chart_bg);
        lineDataSet.setFillDrawable(drawable);
        lineDataSet.setCircleColor(ActivityCompat.getColor(this, R.color.colorAccent));

        // 设置圆点的颜色
        //  lineDataSet.setCircleColor(getResources().getColor(R.color.bt_zise));
        //  lineDataSet.setDrawCircleHole(false);
        LineData lineData = new LineData(lineDataSet);
        lineChart.setData(lineData);
        lineChart.invalidate();
        lineChart.animateXY(1500, 1500, Easing.EasingOption.EaseInSine, Easing.EasingOption.EaseInSine);


    }

    @Override
    protected void onStart() {
        super.onStart();
        get_eth_balance(address, tokenAddress, tokenDecimal, tokenSymbol);
    }

    @OnClick({R.id.back_bt, R.id.transfer_bt, R.id.code_bt})
    public void onViewClicked(View view) {
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.back_bt:
                finish();
                break;
            case R.id.transfer_bt:

                bundle.putString("address", "" + address);
                bundle.putString("tokenAddress", tokenAddress);
                bundle.putString("tokenSymbol", tokenSymbol);
                bundle.putInt("tokenDecimal", tokenDecimal);
                RxActivityTool.skipActivity(this, BTransferActivity.class, bundle);
                break;
            case R.id.code_bt:
                bundle.putString("address", String.valueOf(address));
                RxActivityTool.skipActivity(this, BReceivableCodeActivity.class, bundle);
                break;

        }
    }

    //获取ETH,代币余额
    public void get_eth_balance(String ethAddress,
                                String ethTokenAddress,
                                Integer ethTokenDecimal,
                                String ethTokenSymbol
    ) {
        EthNetImpl.get_eth_balance(ethAddress, ethTokenAddress, ethTokenDecimal, ethTokenSymbol, ethAddress+ethTokenAddress + "_get_eth_balance",
                new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (response.isSuccessful() && !isFinishing()) {
                            GetEthBalanceDomain domain = new Gson().fromJson(response.body(), GetEthBalanceDomain.class);
                            switch (domain.getStatus()) {
                                case BaseApi.REQUEST_SUCCESS_CODE:
                                    headTotal.setText(String.valueOf(domain.getData().getBalance()));
                                    headTotalSum.setText(String.valueOf("≈ ¥ " + domain.getData().getCny()));
                                    break;
                                default:
                                    headTotal.setText("0");
                                    headTotalSum.setText(String.valueOf("≈ ¥ 0"));
                                    break;
                            }
                        }
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        if (response.isSuccessful() && !isFinishing()) {
                            GetEthBalanceDomain domain = new Gson().fromJson(response.body(), GetEthBalanceDomain.class);
                            switch (domain.getStatus()) {
                                case BaseApi.REQUEST_SUCCESS_CODE:
                                    headTotal.setText(String.valueOf(domain.getData().getBalance()));
                                    headTotalSum.setText(String.valueOf("≈ ¥ " + domain.getData().getCny()));
                                    break;
                                default:
                                    headTotal.setText("0");
                                    headTotalSum.setText(String.valueOf("≈ ¥ 0"));
                                    break;
                            }
                        }
                    }
                });
    }


    public void initRefresh(String tokenAddress, String ethAddress, Integer pageNow, Integer pageSize) {
        EthNetImpl
                .query_transfer_by_address(tokenAddress, ethAddress, pageNow, pageSize, ethAddress+tokenAddress + pageNow + "_query_transfer_by_address",
                        new StringCallback() {
                            @Override
                            public void onFinish() {
                                super.onFinish();
                                twinklingRefresh.finishRefreshing();
                            }

                            @Override
                            public void onSuccess(Response<String> response) {
                                if (response.isSuccessful() && !isFinishing()) {
                                    EthTransferHistoryDomain domain = new Gson().fromJson(response.body(), EthTransferHistoryDomain.class);
                                    switch (domain.getStatus()) {
                                        case BaseApi.REQUEST_SUCCESS_CODE:
                                            if (domain.getData() != null && domain.getData().size() != 0) {
                                                noDataTv.setVisibility(View.GONE);
                                                adapter.refreshList(domain.getData());
                                               List<EthTransfer> transfers=adapter.dataBeanList;
                                                List<Entry> entryList = new ArrayList<>();
                                                for (int i = 0; i < transfers.size(); i++) {
                                                    EthTransfer transfer=transfers.get(i);
                                                    Entry entry=null;
                                                    if(address.equals(transfer.getFromAddress())){
                                                         entry = new Entry(i,-Float.parseFloat(transfer.getAmount()));
                                                    }else{
                                                        entry = new Entry(i,Float.parseFloat(transfer.getAmount()));
                                                    }
                                                    entryList.add(entry);
                                                }

                                                XAxis xAxis = lineChart.getXAxis();
                                                xAxis.setDrawGridLines(false);
                                                /*解决左右两端柱形图只显示一半的情况 只有使用CombinedChart时会出现，如果单独使用BarChart不会有这个问题*/
//        xAxis.setAxisMinimum(0);
//        xAxis.setAxisMaximum(10);
                                                //   xAxis.setLabelCount(4);
                                                xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); // 设置X轴标签位置，BOTTOM在底部显示，TOP在顶部显示
                                                //    xAxis.setLabelCount(domain.getData().getList().size()); // 设置X轴标签数量
                                                xAxis.setValueFormatter(new IAxisValueFormatter() {
                                                    @Override
                                                    public String getFormattedValue(float value, AxisBase axis) {
                                                        int v = (int) value;

                                                        if(v<=transfers.size()-1){
                                                            EthTransfer  tr=transfers.get(v);
                                                            SimpleDateFormat sdf= new SimpleDateFormat("MM/dd");
                                                            return sdf.format(new Date(Long.parseLong(tr.getSendTime())));
                                                        }else{
                                                            return "";
                                                        }
//                                                        SimpleDateFormat sdf= new SimpleDateFormat("MM/dd");

                                                    }
                                                });
                                                YAxis axisLeft = lineChart.getAxisLeft(); // 获取左边Y轴操作类
                                                //   axisLeft.setAxisMinimum(0); // 设置最小值
                                                //                            axisLeft.setGranularityEnabled(true);
//                              axisLeft.setGranularity(10); // 设置Label间隔
                                                //  axisLeft.setLabelCount(10);// 设置标签数量
                                                axisLeft.setDrawGridLines(false);
                                                YAxis rightYAxis = lineChart.getAxisRight();
                                                rightYAxis.setEnabled(false);
                                                rightYAxis.setDrawGridLines(true);
                                                rightYAxis.enableGridDashedLine(10f, 10f, 0f);
                                                axisLeft.resetAxisMinimum();
                                                axisLeft.resetAxisMaximum();
                                                LineDataSet lineDataSet = new LineDataSet(entryList, getString(R.string.transaction));
                                                lineDataSet.setDrawCircleHole(false);
                                                // 设置平滑曲线
                                                lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
                                                // 不显示坐标点的小圆点
                                                lineDataSet.setDrawCircles(false);
                                                lineDataSet.setDrawValues(false);
                                                lineDataSet.setFillColor(ActivityCompat.getColor(BHistoryActivity.this, R.color.colorAccent));
                                                lineDataSet.setColor(ActivityCompat.getColor(BHistoryActivity.this, R.color.colorAccent));
                                                //设置填充
                                                //设置允许填充
                                                lineDataSet.setDrawFilled(true);
                                                lineDataSet.setFillAlpha(50);
                                                //设置渐变
                                                Drawable drawable = ContextCompat.getDrawable(BHistoryActivity.this, R.drawable.line_chart_bg);
                                                lineDataSet.setFillDrawable(drawable);
                                                lineDataSet.setCircleColor(ActivityCompat.getColor(BHistoryActivity.this, R.color.colorAccent));

                                                // 设置圆点的颜色
                                                //  lineDataSet.setCircleColor(getResources().getColor(R.color.bt_zise));
                                                //  lineDataSet.setDrawCircleHole(false);
                                                LineData lineData = new LineData(lineDataSet);
                                                lineChart.setData(lineData);
                                                lineChart.invalidate();
                                                lineChart.animateXY(1500, 1500, Easing.EasingOption.EaseInSine, Easing.EasingOption.EaseInSine);
                                            }else{
                                                adapter.clearList();
                                                noDataTv.setVisibility(View.VISIBLE);
                                            }
                                            break;

                                         default:
                                             adapter.clearList();
                                             noDataTv.setVisibility(View.VISIBLE);
                                             break;
                                    }
                                }


                            }

                            @Override
                            public void onCacheSuccess(Response<String> response) {
                                if (response.isSuccessful() && !isFinishing()) {
                                    EthTransferHistoryDomain domain = new Gson().fromJson(response.body(), EthTransferHistoryDomain.class);
                                    switch (domain.getStatus()) {
                                        case BaseApi.REQUEST_SUCCESS_CODE:
                                            if (domain.getData() != null && domain.getData().size() != 0) {
                                                noDataTv.setVisibility(View.GONE);
                                                adapter.refreshList(domain.getData());
                                                List<EthTransfer> transfers=adapter.dataBeanList;
                                                List<Entry> entryList = new ArrayList<>();
                                                for (int i = 0; i < transfers.size(); i++) {
                                                    EthTransfer transfer=transfers.get(i);
                                                    Entry entry=null;
                                                    if(address.equals(transfer.getFromAddress())){
                                                        entry = new Entry(i,-Float.parseFloat(transfer.getAmount()));
                                                    }else{
                                                        entry = new Entry(i,Float.parseFloat(transfer.getAmount()));
                                                    }
                                                    entryList.add(entry);
                                                }

                                                XAxis xAxis = lineChart.getXAxis();
                                                xAxis.setDrawGridLines(false);
                                                /*解决左右两端柱形图只显示一半的情况 只有使用CombinedChart时会出现，如果单独使用BarChart不会有这个问题*/
//        xAxis.setAxisMinimum(0);
//        xAxis.setAxisMaximum(10);
                                                //   xAxis.setLabelCount(4);
                                                xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); // 设置X轴标签位置，BOTTOM在底部显示，TOP在顶部显示
                                                //    xAxis.setLabelCount(domain.getData().getList().size()); // 设置X轴标签数量
                                                xAxis.setValueFormatter(new IAxisValueFormatter() {
                                                    @Override
                                                    public String getFormattedValue(float value, AxisBase axis) {
                                                        int v = (int) value;

                                                             if(v<=transfers.size()-1){
                                                                 EthTransfer  tr=transfers.get(v);
                                                                 SimpleDateFormat sdf= new SimpleDateFormat("MM/dd");
                                                                 return sdf.format(new Date(Long.parseLong(tr.getSendTime())));
                                                             }else{
                                                                 return "";
                                                             }
//                                                        SimpleDateFormat sdf= new SimpleDateFormat("MM/dd");

                                                    }
                                                });
                                                YAxis axisLeft = lineChart.getAxisLeft(); // 获取左边Y轴操作类
                                                //   axisLeft.setAxisMinimum(0); // 设置最小值
                                                //                            axisLeft.setGranularityEnabled(true);
//                              axisLeft.setGranularity(10); // 设置Label间隔
                                                //  axisLeft.setLabelCount(10);// 设置标签数量
                                                axisLeft.setDrawGridLines(false);
                                                YAxis rightYAxis = lineChart.getAxisRight();
                                                rightYAxis.setEnabled(false);
                                                rightYAxis.setDrawGridLines(true);
                                                rightYAxis.enableGridDashedLine(10f, 10f, 0f);
                                                axisLeft.resetAxisMinimum();
                                                axisLeft.resetAxisMaximum();
                                                LineDataSet lineDataSet = new LineDataSet(entryList, getString(R.string.transaction));
                                                lineDataSet.setDrawCircleHole(false);
                                                // 设置平滑曲线
                                                lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
                                                // 不显示坐标点的小圆点
                                                lineDataSet.setDrawCircles(false);
                                                lineDataSet.setDrawValues(false);
                                                lineDataSet.setFillColor(ActivityCompat.getColor(BHistoryActivity.this, R.color.colorAccent));
                                                lineDataSet.setColor(ActivityCompat.getColor(BHistoryActivity.this, R.color.colorAccent));
                                                //设置填充
                                                //设置允许填充
                                                lineDataSet.setDrawFilled(true);
                                                lineDataSet.setFillAlpha(50);
                                                //设置渐变
                                                Drawable drawable = ContextCompat.getDrawable(BHistoryActivity.this, R.drawable.line_chart_bg);
                                                lineDataSet.setFillDrawable(drawable);
                                                lineDataSet.setCircleColor(ActivityCompat.getColor(BHistoryActivity.this, R.color.colorAccent));

                                                // 设置圆点的颜色
                                                //  lineDataSet.setCircleColor(getResources().getColor(R.color.bt_zise));
                                                //  lineDataSet.setDrawCircleHole(false);
                                                LineData lineData = new LineData(lineDataSet);
                                                lineChart.setData(lineData);
                                                lineChart.invalidate();
                                                lineChart.animateXY(1500, 1500, Easing.EasingOption.EaseInSine, Easing.EasingOption.EaseInSine);
                                            }else{
                                                adapter.clearList();
                                                noDataTv.setVisibility(View.VISIBLE);
                                            }
                                            break;

                                        default:
                                            adapter.clearList();
                                            noDataTv.setVisibility(View.VISIBLE);
                                            break;
                                    }
                                }


                            }
                        });
    }

    public void initAppend(String tokenAddress, String ethAddress, Integer pageNow, Integer pageSize) {
        EthNetImpl
                .query_transfer_by_address(tokenAddress, ethAddress, pageNow, pageSize,  "query_transfer_by_address",
                        new StringCallback() {
                            @Override
                            public void onStart(Request<String, ? extends Request> request) {
                                super.onStart(request);
                                isMoreLoading = true;
                            }

                            @Override
                            public void onFinish() {
                                super.onFinish();
                                isMoreLoading = false;
                            }

                            @Override
                            public void onSuccess(Response<String> response) {
                                if (response.isSuccessful() && !isFinishing()) {
                                    EthTransferHistoryDomain domain = new Gson().fromJson(response.body(), EthTransferHistoryDomain.class);
                                    switch (domain.getStatus()) {
                                        case BaseApi.REQUEST_SUCCESS_CODE:
                                            if (domain.getData() != null && domain.getData().size() != 0) {
                                                noDataTv.setVisibility(View.GONE);
                                                adapter.appendList(domain.getData());
                                                List<EthTransfer> transfers=adapter.dataBeanList;
                                                List<Entry> entryList = new ArrayList<>();
                                                for (int i = 0; i < transfers.size(); i++) {
                                                    EthTransfer transfer=transfers.get(i);
                                                    Entry entry=null;
                                                    if(address.equals(transfer.getFromAddress())){
                                                        entry = new Entry(i,-Float.parseFloat(transfer.getAmount()));
                                                    }else{
                                                        entry = new Entry(i,Float.parseFloat(transfer.getAmount()));
                                                    }
                                                    entryList.add(entry);
                                                }

                                                XAxis xAxis = lineChart.getXAxis();
                                                xAxis.setDrawGridLines(false);
                                                /*解决左右两端柱形图只显示一半的情况 只有使用CombinedChart时会出现，如果单独使用BarChart不会有这个问题*/
//        xAxis.setAxisMinimum(0);
//        xAxis.setAxisMaximum(10);
                                                //   xAxis.setLabelCount(4);
                                                xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); // 设置X轴标签位置，BOTTOM在底部显示，TOP在顶部显示
                                                //    xAxis.setLabelCount(domain.getData().getList().size()); // 设置X轴标签数量
                                                xAxis.setValueFormatter(new IAxisValueFormatter() {
                                                    @Override
                                                    public String getFormattedValue(float value, AxisBase axis) {
                                                        int v = (int) value;

                                                        if(v<=transfers.size()-1){
                                                            EthTransfer  tr=transfers.get(v);
                                                            SimpleDateFormat sdf= new SimpleDateFormat("MM/dd");
                                                            return sdf.format(new Date(Long.parseLong(tr.getSendTime())));
                                                        }else{
                                                            return "";
                                                        }
//                                                        SimpleDateFormat sdf= new SimpleDateFormat("MM/dd");

                                                    }
                                                });
                                                YAxis axisLeft = lineChart.getAxisLeft(); // 获取左边Y轴操作类
                                                //   axisLeft.setAxisMinimum(0); // 设置最小值
                                                //                            axisLeft.setGranularityEnabled(true);
//                              axisLeft.setGranularity(10); // 设置Label间隔
                                                //  axisLeft.setLabelCount(10);// 设置标签数量
                                                axisLeft.setDrawGridLines(false);
                                                YAxis rightYAxis = lineChart.getAxisRight();
                                                rightYAxis.setEnabled(false);
                                                rightYAxis.setDrawGridLines(true);
                                                rightYAxis.enableGridDashedLine(10f, 10f, 0f);
                                                axisLeft.resetAxisMinimum();
                                                axisLeft.resetAxisMaximum();
                                                LineDataSet lineDataSet = new LineDataSet(entryList, getString(R.string.transaction));
                                                lineDataSet.setDrawCircleHole(false);
                                                // 设置平滑曲线
                                                lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
                                                // 不显示坐标点的小圆点
                                                lineDataSet.setDrawCircles(false);
                                                lineDataSet.setDrawValues(false);
                                                lineDataSet.setFillColor(ActivityCompat.getColor(BHistoryActivity.this, R.color.colorAccent));
                                                lineDataSet.setColor(ActivityCompat.getColor(BHistoryActivity.this, R.color.colorAccent));
                                                //设置填充
                                                //设置允许填充
                                                lineDataSet.setDrawFilled(true);
                                                lineDataSet.setFillAlpha(50);
                                                //设置渐变
                                                Drawable drawable = ContextCompat.getDrawable(BHistoryActivity.this, R.drawable.line_chart_bg);
                                                lineDataSet.setFillDrawable(drawable);
                                                lineDataSet.setCircleColor(ActivityCompat.getColor(BHistoryActivity.this, R.color.colorAccent));

                                                // 设置圆点的颜色
                                                //  lineDataSet.setCircleColor(getResources().getColor(R.color.bt_zise));
                                                //  lineDataSet.setDrawCircleHole(false);
                                                LineData lineData = new LineData(lineDataSet);
                                                lineChart.setData(lineData);
                                                lineChart.invalidate();
                                                lineChart.animateXY(1500, 1500, Easing.EasingOption.EaseInSine, Easing.EasingOption.EaseInSine);
                                            }
                                            break;
                                    }
                                }


                            }

                        });
    }
}
